home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / shoot / athrust.lha / AmigaThrust / helpers / bin2c.c next >
Encoding:
C/C++ Source or Header  |  1998-09-05  |  2.7 KB  |  169 lines

  1.  
  2. /* Written by Peter Ekberg, peda@lysator.liu.se */
  3. /* Sound sample support by Frank Wille, frank@phoenix.owl.de */
  4.  
  5. #ifdef HAVE_CONFIG_H
  6. # include "../src/config.h"
  7. #endif
  8.  
  9. #ifdef HAVE_UNISTD_H
  10. # include <unistd.h>
  11. #endif
  12.  
  13. #ifndef AMIGA
  14. #include <sys/types.h>
  15. #endif
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. #define LL (8)
  20.  
  21. void quit(char *str);
  22. int writebuf(int count);
  23.  
  24. unsigned char buf[LL+1];
  25. static int len;
  26.  
  27.  
  28. void
  29. quit(char *str)
  30. {
  31.   perror(str);
  32.   exit(1);
  33. }
  34.  
  35. int
  36. writebuf(int count)
  37. {
  38.   int i;
  39.   
  40.   len += count;
  41. #if PRINTF_RETURN >= 0
  42.   if(
  43. #endif
  44.      printf("\t")
  45. #if PRINTF_RETURN == 1
  46.                   != 1
  47. #endif
  48. #if PRINTF_RETURN >= 0
  49.                       )
  50.     return(1)
  51. #endif
  52.       ;
  53.  
  54.   for(i=0; i<count-1; i++)
  55. #if PRINTF_RETURN >= 0
  56.     if(
  57. #endif
  58.        printf("0x%02x, ", buf[i])
  59. #if PRINTF_RETURN == 1
  60.                                   != 6
  61. #endif
  62. #if PRINTF_RETURN >= 0
  63.                                       )
  64.       return(1)
  65. #endif
  66.       ;
  67.  
  68.   if(i<count)
  69. #if PRINTF_RETURN >= 0
  70.     if(
  71. #endif
  72.        printf("0x%02x", buf[i])
  73. #if PRINTF_RETURN == 1
  74.                                 != 4
  75. #endif
  76. #if PRINTF_RETURN >= 0
  77.                                     )
  78.       return(1)
  79. #endif
  80.       ;
  81.  
  82.   return(0);
  83. }
  84.  
  85. int
  86. main(int argc, char *argv[])
  87. {
  88.   int stat;
  89.   int end=0;
  90.   FILE *si;
  91.  
  92.   len = 0;
  93.   if(argc!=2) {
  94.     fprintf(stderr, "%s: Usage '%s variable_name'\n",
  95.             argv[0],
  96.             argv[0]);
  97.     exit(1);
  98.   }
  99.  
  100. #ifdef AMIGA
  101.   si = stdin;
  102. #else
  103.   si = fdopen(fileno(stdin), "rb");
  104. #endif
  105.  
  106. #if PRINTF_RETURN >= 0
  107.   if(
  108. #endif
  109.      printf("\nunsigned char %s[] = {\n", argv[1])
  110. #if PRINTF_RETURN == 1
  111.                                                    != (22 + strlen(argv[1]))
  112. #endif
  113. #if PRINTF_RETURN >= 0
  114.                                                                             )
  115.     quit(argv[0])
  116. #endif
  117.       ;
  118.   stat=fread(buf+LL, 1, 1, si);
  119.   if(stat!=1) {
  120.     if(ferror(si))
  121.       quit(argv[0]);
  122.     end=1;
  123.   }
  124.   while(!end) {
  125.     *buf=*(buf+LL);
  126.     stat=fread(buf+1, 1, LL, si);
  127.     if(stat!=LL) {
  128.       if(ferror(si))
  129.         quit(argv[0]);
  130.       end=1;
  131.       if(writebuf(stat+1))
  132.         quit(argv[0]);
  133.     }
  134.     else {
  135.       if(writebuf(LL))
  136.         quit(argv[0]);
  137. #if PRINTF_RETURN >= 0
  138.       if(
  139. #endif
  140.          printf(",\n")
  141. #if PRINTF_RETURN == 1
  142.                        != 2
  143. #endif
  144. #if PRINTF_RETURN >= 0
  145.                            )
  146.         quit(argv[0])
  147. #endif
  148.           ;
  149.     }
  150.   }  
  151. #if PRINTF_RETURN >= 0
  152.   if(
  153. #endif
  154.      printf(" };\n")
  155. #if PRINTF_RETURN == 1
  156.                      != 4
  157. #endif
  158. #if PRINTF_RETURN >= 0
  159.                          )
  160.     quit(argv[0])
  161. #endif
  162.       ;
  163.  
  164.   if (!strncmp(argv[1],"sound_",6))                 /* phx 08/98 */
  165.     printf("\nunsigned int %s_len = %d;\n",argv[1],len);
  166.  
  167.   return(0);
  168. }
  169.